package cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.impl;

import cn.hutool.core.util.ZipUtil;

import cn.yuanqiao.feignService.RemoteCommonService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.cqbc.dataManage.domain.DataManage;
import cn.yuanqiao.cqbc.dataManage.mapper.DataManageMapper;
import cn.yuanqiao.cqbc.inspection.domain.RegularInspections;
import cn.yuanqiao.cqbc.inspectionLog.domain.InspectionLog;
import cn.yuanqiao.cqbc.inspectionLog.domain.LogDetails;
import cn.yuanqiao.cqbc.inspectionLog.mapper.LogDetailsMapper;
import cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.TaskListService;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: TaskListServiceImpl
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2022/11/14 14:39
 * @Blog:
 */

@Service("inspection")
@Slf4j
public class TaskListInspectionServiceImpl implements TaskListService {

    @Resource
    private DataManageMapper dataManageMapper;

    @Autowired
    private RemoteCommonService remoteCommonService;

    @Autowired
    private LogDetailsMapper logDetailsMapper;

    @Override
    public <T> Map<String,Object> task(T... args) {

        InspectionLog arg = (InspectionLog) args[0];
        RegularInspections regularInspections= (RegularInspections) args[1];
        int suc = 0;
        int err = 0;
        String errMsg = "";
        DataManage dataManage=new DataManage();
        dataManage.setResolved("1");
        dataManage.setTenantCode(regularInspections.getTenantCode());
        dataManage.setCategory(regularInspections.getCategoryId());
        List<DataManage>  yqDataManageResults= dataManageMapper.selectList(dataManage);
        List<LogDetails> res = new ArrayList<>();
        List<Long>sxIds=new ArrayList<>();
        for (DataManage item : yqDataManageResults) {
            //TODO 巡检 做四性检测
                Map<String, Object> categoryIdByMap = null;
                try {
                    JSONObject json=new JSONObject(1<<3);
                    json.put("path",item.getPath()+ File.separator+item.getName()+"."+item.getFormat());
                    json.put("code",regularInspections.getSchemeCode());
                    json.put("link","2");
                    json.put("lx","1");
                    AjaxResult data = remoteCommonService.detectionPackage(json);
                    int  failNum= (Integer) data.get("failNum");
                    String sxId=(String) data.get("sxId");
                    LogDetails logDetails = new LogDetails();
                    logDetails.setId(SnowflakeIdGenerator.generateId());
                    logDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    logDetails.setDataId(item.getId());
                    logDetails.setDataPath(item.getPath());
                    logDetails.setTitle(item.getTitle());
                    logDetails.setDh(item.getName());//TODO 插入四性详细数据
                    logDetails.setpId(arg.getId());
                    logDetails.setTableName(regularInspections.getTenantCode()+"_YQ_INSPECTION_LOG_DETAILS");
                    logDetails.setDetailsId(Long.valueOf(sxId));
                    sxIds.add(Long.valueOf(sxId));
                    //TODO 检测档案解压eep包的创建时间和包最后修改时间是否一致
                    boolean flag = true;
                    //通过当前检测包档号获取包MD5内容

                    //TODO 关闭巡检md5校验功能
                    flag=true;
                    if (failNum==0){
                        suc++;
                        logDetails.setDetectionResult("1");
                    }else {
                        err++;
                        logDetails.setDetectionResult("0");
                    }
                    res.add(logDetails);
                } catch (Exception e) {
                    e.printStackTrace();
                }


        }


        Map result = new HashMap<String,Object>();
        //查看所有的失败信息
        String msg=logDetailsMapper.getErroMsg(sxIds,regularInspections.getTenantCode().toUpperCase());


        result.put("msg","{success:"+suc+",error:"+err+",msg:"+msg+"}");
        result.put("detail",res);
        return result;
    }
}
